package com.sk.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sk.entity.Users;
import com.sk.mapper.UsersMapper;
import com.sk.service.IUsersService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sk.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author smith
 * @since 2025-05-27
 */
@Service
public class UsersServiceImpl  implements IUsersService {

    @Autowired
    private UsersMapper usersMapper;

    @Override
    public Result saveuser(Users user) {
        // insert into users ....
        // rows 表示 users表中受影响行数 , rows = 1 表示 添加了一条记录成功
        int rows = usersMapper.insert(user);
        if(rows >0)
            return Result.saveSuccess(user);
        return Result.saveFail(user);
    }

    @Override
    public Result getById(Integer userid) {
        //select * from users where user_id
        Users user = usersMapper.selectById(userid);
        if(user != null)
            return Result.getSuccess(user);
        return Result.getFail(null);
    }


    @Override
    public Result login(String userName, String password) {
        QueryWrapper<Users> wrapper = new QueryWrapper<>();
        wrapper.eq("user_name", userName);
        wrapper.eq("user_password", password);

        Users user = usersMapper.selectOne(wrapper);

        if (user != null) {
            return Result.getSuccess(user); // 登录成功
        } else {
            return Result.getFail("用户名或密码错误"); // 登录失败
        }
    }


}
